$(function () {

    $('body')
        .on('click', '.classesTopic, .refreshClassesTopic,#btnTopic', function () { // 搜索
            listClassesTopic(1);
        })
        .on('click', '.topic-fa-arrow-up', function () { // 置顶
            var ids = $(this).parents('tr').data('id');
            updateTopicTop(ids, 1);
        })
        .on('click', '.look', function () { // 查看话题
            var id = $(this).data('id');
            openDetails(id);
        })
        .on('click', '.update', function () { // 修改话题
            var id = $(this).data('id');
           var url= baseloaction + '/admin/classes/topic/initUpdate?id=' + id + "&crumbs=true";
            updateDetails(url);
        })
        .on('click', '#saveTaskType4', function () { // 新建话题
            var url= baseloaction + '/admin/classes/topic/initAdd?crumbs=true';
            updateDetails(url);
        })
        .on('click', '.topic-fa-arrow-down', function () { // 取消置顶
            var ids = $(this).parents('tr').data('id');
            updateTopicTop(ids, 2);
        })
        .on('click', '#publishBatch', function () { // 批量发布
            icModale(2, '确认批量恢复选中的话题', '', function () {
                var checked = $('input[name=id]:checked');
                updateClassesTopicStatus(getChecked(1, checked), 1);
            });
        })
        .on('click', '#frozenBatch', function () { // 批量禁用
            icModale(2, '确认批量禁用选中的话题', '', function () {
                var checked = $('input[name=id]:checked');
                updateClassesTopicStatus(getChecked(2, checked), 2);
            });
        })
        .on('click', '#deleteBatch', function () { // 批量删除
            icModale(2, '确认批量删除选中的话题', '', function () {
                var checked = $('input[name=id]:checked');
                updateClassesTopicStatus(getChecked(3, checked), 3);
            });
        })
        .on('click', '.topic-fa-unlock-alt', function () { // 单个禁用
            var ids = $(this).parents('tr').data('id');
            icModale(2, '确认禁用选中的话题', '', function () {
                updateClassesTopicStatus(ids, 2);
            });
        })
        .on('click', '.topic-fa-history', function () { // 单个恢复
            var ids = $(this).parents('tr').data('id');
            icModale(2, '确认恢复选中的话题', '', function () {
                updateClassesTopicStatus(ids, 1);
            });
        })
        .on('click', '.topic-fa-trash', function () { // 单个删除
            var ids = $(this).parents('tr').data('id');
            icModale(2, '确认删除选中的话题', '', function () {
                updateClassesTopicStatus(ids, 3);
            });
        });


    function updateTopicTop(ids, top) {
        if (!ids) {
            icModale(0, '请选择要操作的话题');
            return;
        }
        $.ajax({
            url: baseloaction + '/admin/classes/topic/updateTopicTop',
            data: {'ids': ids, 'top': top},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                icModale(0, response.message);
                if (response.success) {
                    var array = (ids + "").split(",");
                    array.forEach(function (value) {
                        var tr = $('tr[data-id=' + value + ']');
                        if (top == 1) {
                            tr.find('i.fa-arrow-up').attr('class', 'fa fa-arrow-down')
                                .parent('a').attr('data-original-title', '取消置顶');
                        } else {
                            tr.find('i.fa-arrow-down').attr('class', 'fa fa-arrow-up')
                                .parent('a').attr('data-original-title', '置顶');
                        }

                    });
                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        });
    }

    /**
     * 更新话题状态
     * @param ids
     * @param status 1.发布 2.禁用 3.删除
     */
    function updateClassesTopicStatus(ids, status) {
        if (!ids) {
            icModale(0, '请选择要操作的话题');
            return;
        }
        if (ids == '-1') {
            if (status == 2) {
                icModale(0, '所选话题已禁用，操作无效');
                return;
            } else if (status == 1) {
                icModale(0, '所选话题正常，操作无效');
                return;
            }
        }
        $.ajax({
            url: baseloaction + '/admin/classes/topic/updateTopicStatus',
            data: {'ids': ids, 'status': status},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    if (status != 3) {
                        icModale(0, response.message);
                    }
                    var array = (ids + "").split(",");
                    array.forEach(function (value) {
                        updatePage(value, status);
                    });
                } else {
                    icModale(0, response.message);
                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        });
    }

    /**
     * 更新页面
     * @param id 试题题型(模板)id
     * @param status 1.发布 2.禁用 3.删除
     */
    function updatePage(id, status) {
        var tr = $('tr[data-id=' + id + ']');
        switch (status) {
            case 1:
                tr.find('i.fa-history').attr('class', 'fa fa-unlock-alt').parent('a').attr('data-original-title', '禁用');
                tr.find('td[data-status]').html('<span class="label label-info">正常</span>');
                break;
            case 2:
                if (tr.find('i.fa-unlock-alt').length > 0) {
                    tr.find('i.fa-unlock-alt').attr('class', 'fa fa-history').parent('a').attr('data-original-title', '恢复');
                    tr.find('td[data-status]').html('<span class="label label-danger">禁用</span>');
                }
                break;
            case 3:
                if (tr.find('i.fa-trash').length > 0) {
                    tr.remove();
                }
                break;
        }
    }

});

function updateDetails(url) {
    parent.layer.open({
        type: 2,
        area: winArea,
        maxmin: true, //开启最大化最小化按钮
        btn: ["关闭"],
        content: url,
        shade: [0.3, '#000000'],
        shadeClose: false,
        title: '<strong>话题修改</strong><small class="ml10"></small>',
        yes: function (index, layero) {
            parent.layer.close(index);
        },
        end:function () {
            $(".refreshClassesTopic").click();
        }
    });
}

function openDetails(topicId) {
    parent.layer.open({
        type: 2,
        area: winArea,
        maxmin: true, //开启最大化最小化按钮
        btn: ["关闭"],
        content: baseloaction + '/admin/classes/topic/previewTopic?id=' + topicId + "&crumbs=true",
        shade: [0.3, '#000000'],
        shadeClose: false,
        title: '<strong>话题详情</strong><small class="ml10"></small>',
        yes: function (index, layero) {
            parent.layer.close(index);
        }
    });
}

function getClassesId() {
    return $('#classesId').val();
}

/**
 * 加载班级话题数据
 * @param pageNo
 */
function listClassesTopic(pageNo) {
    var params = $('.search-form4').serializeJSON();
    params.classesId = getClassesId();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/classes/topic/ajaxClassesTopicListPage',//请求URL
        contextId: 'oc-topic-content',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: listClassesTopic, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: 'all-select',        //全选框ID
        callback: function () {
            $('#count4').html(total);
        }
    };
    ajaxGetListData(obj);
}